<template>
  <div class="mainBox">
    <pageTitle Title="广告管理"></pageTitle>
    <div class="searchBar">
      <el-button @click="handleOpen('add')">添加</el-button>
    </div>

    <div class="tableBar">
      <MyTable :tableData="tableList" :border="true" height="64rem">
        <el-table-column prop="id" label="编号" align="center" width="80" />
        <el-table-column label="类型" align="center" width="120">
          <template #default="scope">
            {{ scope.row.type == 1 ? '通知' : '广告' }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" align="center" />
        <el-table-column prop="content" label="内容" align="center" />
        <el-table-column prop="link" label="跳转链接地址" align="center" />
        <el-table-column prop="loc" label="内容" align="center" />
        <el-table-column prop="content" label="广告发布位置" align="center" />
        <el-table-column label="类型" align="center">
          <template #default="scope">
            {{ scope.row.state == 1 ? '发布' : '不发布' }}
          </template>
        </el-table-column>
        <el-table-column prop="order" label="排序" align="center" />
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template #default="scope">
            <el-button @click="handleOpen('edit', scope.row)">编 辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)"
              >删 除</el-button
            >
          </template>
        </el-table-column>
      </MyTable>
    </div>

    <adDialog
      :show="adDia"
      :adType="adType"
      :adData="adData"
      @closeDia="closeDia"
      @handleSubmit="handleSubmit"
      :key="adDia"
    ></adDialog>
  </div>
</template>

<script setup>
import { addAd, deleteAd, updateAd, adList } from '@/Api/pc';
import { ElMessage } from 'element-plus';
import { useMessage } from '@/utils/utils';

const tableList = ref([]);
const adDia = ref(false);
const adType = ref('');
let adData = reactive({});

// 打开弹窗确定弹窗类型
const handleOpen = (type, data) => {
  adType.value = type;
  adData = data;
  adDia.value = true;
};

// 删除问卷回答
const handleDelete = (id) => {
  useMessage('确定删除广告信息？', () => {
    deleteAd({
      id,
    }).then((res) => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      });
      getAdList();
    });
  });
};

//获取广告
const getAdList = () => {
  adList().then((res) => {
    tableList.value = res.obj;
  });
};

const closeDia = () => {
  adDia.value = false;
};

const handleSubmit = (row) => {
  if (adType.value == 'add') {
    addAd(row).then((res) => {
      getAdList();
    });
  } else {
    updateAd(row).then((res) => {
      getAdList();
    });
  }
};

onMounted(() => {
  getAdList();
});
</script>

<style lang="scss" scoped>
.tableBar {
  display: flex;
  justify-content: space-between;
  height: calc(100% - 9rem);
  padding: 1.5rem 0.5rem;
}
@import '@/assets/css/mycss.css';
</style>
